package cn.com.nxt.txb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.com.nxt.txb.R;
import cn.com.nxt.txb.model.ApplyMeeting;
import cn.com.nxt.txb.util.OnApplyItemClickListener;


public class ApplyAdapter extends RecyclerView.Adapter<ApplyAdapter.MyViewHolder> {

	public ApplyAdapter(Context mContext, List<ApplyMeeting> mDatas) {
		super();
		this.mContext = mContext;
		this.mDatas = mDatas;
	}

	private Context mContext;
	protected List<ApplyMeeting> mDatas;

	private OnApplyItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnApplyItemClickListener listener) {
		this.mOnItemClickListener = listener;
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_apply, parent, false));
		return holder;
	}

	@Override
	public void onBindViewHolder(final MyViewHolder holder, final int position) {
		ApplyMeeting record=mDatas.get(position);
		holder.tvName.setText(record.getUsername());
		if(record.getStatus()==0){
			holder.llApply.setVisibility(View.VISIBLE);
			holder.tvAlready.setVisibility(View.GONE);
			holder.tvMic.setVisibility(View.GONE);
			setItemViewEvent(holder,holder.tvAgreen,0);
			setItemViewEvent(holder,holder.tvRefuse,1);
		}else if(record.getStatus()==1){
			holder.llApply.setVisibility(View.GONE);
			holder.tvAlready.setVisibility(View.VISIBLE);
			holder.tvMic.setVisibility(View.GONE);
		}else if(record.getStatus()==2){
			holder.llApply.setVisibility(View.GONE);
			holder.tvAlready.setVisibility(View.GONE);
			holder.tvMic.setVisibility(View.VISIBLE);
			setItemViewEvent(holder,holder.tvMic,2);
		}
		//holder.tvphone.setText(record.getPhone());
		//holder.tvTime.setText(record.getTime());
		// 绑定每一个itemView的事件  抽取方法 供子类复用
		//setItemViewEvent(holder);
	}

	protected void setItemViewEvent(final MyViewHolder holder,final View view,final int type) {
		if (mOnItemClickListener != null) {
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int realPos = holder.getLayoutPosition();
					mOnItemClickListener.OnItemClick(view, realPos,type);
				}
			});
		}
	}

//	protected void setItemViewEvent(final MyViewHolder holder) {
//		if (mOnItemClickListener != null) {
//			holder.itemView.setOnClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					int realPos = holder.getLayoutPosition();
//					mOnItemClickListener.OnItemClick(holder.itemView, realPos);
//				}
//			});
//		}
//	}

	@Override
	public int getItemCount() {
		return mDatas.size();
	}

//	void louisAdd(int pos) {
//		mDatas.add(pos, "插入1条数据1");
//		this.notifyItemInserted(pos);
//	}

	void louisDelete(int pos) {
		mDatas.remove(pos);
		this.notifyItemRemoved(pos);
	}

	class MyViewHolder extends ViewHolder {
		TextView tvName;
		LinearLayout llApply;
		TextView tvAgreen;
		TextView tvRefuse;
		TextView tvAlready;
		TextView tvMic;

		public MyViewHolder(View view) {
			super(view);
			tvName = (TextView) view.findViewById(R.id.tv_name);
			llApply= (LinearLayout) view.findViewById(R.id.ll_apply);
			tvAgreen = (TextView) view.findViewById(R.id.tv_agree);
			tvRefuse = (TextView) view.findViewById(R.id.tv_refuse);
			tvAlready = (TextView) view.findViewById(R.id.tv_already);
			tvMic = (TextView) view.findViewById(R.id.tv_mic);
		}
	}
}
